package com.ax.api.route.base.drugproducer;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/drug")
public class DrugRoute {

	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	   /**
		 * 菜单页面的跳转
		 */
		@GetMapping("/sys/drug")
		String drugAdministration(Model model) {
		   String url = geturl("/sys/drug");
		   return restTemplate.getForObject(url, String.class);
		}
		
		/**
		 * queryDrug
		 * remote_base
		 * 药品查询(模块:药品)*包含药品的药品分类信息
		 */ 
		@GetMapping("/queryDrug")
		public String queryDrugclass(@RequestParam Map<String,Object> map) {
			String value = "?drugId={drugId}&drugClassCode={drugClassCode}&offset={offset}&limit={limit}";
			String url = geturl("/sys/drug/queryDrug") + value;
			return restTemplate.getForObject(url,String.class,map);
	}
		
	/**
	 * add
	 * remote_base
	 * 药品分类增加(模块:药品分类)*跳转增加页面
	 */   
	@GetMapping("/add/drug")
	public String add(){
	    String url = geturl("/sys/drug/add");
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * insertBaseDrug
	 * remote_base
	 * 药品保存(模块:药品)
	 */ 
	@GetMapping("/insertBaseDrug")
	public String insertBaseDrugClass(@RequestParam Map<String,Object> map) {
		String value = "?drugId={drugId}&drugName={drugName}&drugAlias={drugAlias}&spec={spec}&firstLetter={firstLetter}&drugClassCode={drugClassCode}&drugClassName={drugClassName}&isHemp={isHemp}&component={component}&properties={properties}&functions={functions}&taboo={taboo}&attention={attention}&producerId={producerId}&producerName={producerName}&usages={usages}&adverseReaction={adverseReaction}&storages={storages}&validityTerm={validityTerm}&barCode={barCode}";
		value += "&approvalNumber={approvalNumber}&instructions={instructions}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drug/insertBaseDrug") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
    /**
	 * edit
	 * remote_base
	 * 药品修改(模块:药品)*跳转修改页面
	 */   
	@GetMapping("/edit/drug")
	public String edit(String id){
		String value = "/id=" + id;
	    String url = geturl("/sys/drug/edit") + value;
		return restTemplate.getForObject(url, String.class,id);
	}
	
	/**
	 * updateBaseDrugClass
	 * remote_base
	 * 药品分类修改(模块:药品分类)
	 */ 
	@GetMapping("/updateBaseDrug")
	public String updateBaseDrug(@RequestParam Map<String,Object> map) {
		String value = "?drugId={drugId}&drugName={drugName}&drugAlias={drugAlias}&spec={spec}&firstLetter={firstLetter}&drugClassCode={drugClassCode}&drugClassName={drugClassName}&isHemp={isHemp}&component={component}&properties={properties}&functions={functions}&taboo={taboo}&attention={attention}&producerId={producerId}&producerName={producerName}&usages={usages}&adverseReaction={adverseReaction}&storages={storages}&validityTerm={validityTerm}&barCode={barCode}";
		value += "&approvalNumber={approvalNumber}&instructions={instructions}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drug/updateBaseDrug") + value;
		return restTemplate.getForObject(url,String.class,map);
  }
	
		private String geturl(String path){
			 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BASE_ROUTE);
			 StringBuffer bf = new StringBuffer("http://");
			 bf.append(serviceInstance.getHost());
			 bf.append(":");
			 bf.append(serviceInstance.getPort());
			 bf.append(path);
			 return bf.toString();
		   }
}
